<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="身份证号" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出生日期" prop="birthDate">
        <el-date-picker clearable
          v-model="queryParams.birthDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择出生日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="民族ID" prop="ethnicityId">
        <el-input
          v-model="queryParams.ethnicityId"
          placeholder="请输入民族ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="政治面貌ID" prop="politicalStatusId">
        <el-input
          v-model="queryParams.politicalStatusId"
          placeholder="请输入政治面貌ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="外语语言ID" prop="foreignLanguageId">
        <el-input
          v-model="queryParams.foreignLanguageId"
          placeholder="请输入外语语言ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专科院校ID" prop="vocationalCollegeId">
        <el-input
          v-model="queryParams.vocationalCollegeId"
          placeholder="请输入专科院校ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专科专业ID" prop="vocationalMajorId">
        <el-input
          v-model="queryParams.vocationalMajorId"
          placeholder="请输入专科专业ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考生类型ID" prop="candidateTypeId">
        <el-input
          v-model="queryParams.candidateTypeId"
          placeholder="请输入考生类型ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通知书邮寄地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入通知书邮寄地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通知书收件人" prop="recipientName">
        <el-input
          v-model="queryParams.recipientName"
          placeholder="请输入通知书收件人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮编" prop="postalCode">
        <el-input
          v-model="queryParams.postalCode"
          placeholder="请输入邮编"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考点盟市ID" prop="examCenterCityId">
        <el-input
          v-model="queryParams.examCenterCityId"
          placeholder="请输入考点盟市ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报考专业ID" prop="examMajorId">
        <el-input
          v-model="queryParams.examMajorId"
          placeholder="请输入报考专业ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="志愿院校ID" prop="volunteerCollegeId">
        <el-input
          v-model="queryParams.volunteerCollegeId"
          placeholder="请输入志愿院校ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="志愿专业ID" prop="volunteerMajorId">
        <el-input
          v-model="queryParams.volunteerMajorId"
          placeholder="请输入志愿专业ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['baoming:candidate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['baoming:candidate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['baoming:candidate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['baoming:candidate:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="candidateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="考生ID" align="center" prop="id" />
      <el-table-column label="身份证号" align="center" prop="idNumber" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="出生日期" align="center" prop="birthDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="民族ID" align="center" prop="ethnicityId" />
      <el-table-column label="政治面貌ID" align="center" prop="politicalStatusId" />
      <el-table-column label="外语语言ID" align="center" prop="foreignLanguageId" />
      <el-table-column label="专科院校ID" align="center" prop="vocationalCollegeId" />
      <el-table-column label="专科专业ID" align="center" prop="vocationalMajorId" />
      <el-table-column label="考生类型ID" align="center" prop="candidateTypeId" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="通知书邮寄地址" align="center" prop="address" />
      <el-table-column label="是否未获得证书" align="center" prop="isCertificateIssued" />
      <el-table-column label="通知书收件人" align="center" prop="recipientName" />
      <el-table-column label="邮编" align="center" prop="postalCode" />
      <el-table-column label="考点盟市ID" align="center" prop="examCenterCityId" />
      <el-table-column label="报考专业ID" align="center" prop="examMajorId" />
      <el-table-column label="志愿院校ID" align="center" prop="volunteerCollegeId" />
      <el-table-column label="志愿专业ID" align="center" prop="volunteerMajorId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['baoming:candidate:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['baoming:candidate:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考生信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="candidateRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker clearable
            v-model="form.birthDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="民族ID" prop="ethnicityId">
          <el-input v-model="form.ethnicityId" placeholder="请输入民族ID" />
        </el-form-item>
        <el-form-item label="政治面貌ID" prop="politicalStatusId">
          <el-input v-model="form.politicalStatusId" placeholder="请输入政治面貌ID" />
        </el-form-item>
        <el-form-item label="外语语言ID" prop="foreignLanguageId">
          <el-input v-model="form.foreignLanguageId" placeholder="请输入外语语言ID" />
        </el-form-item>
        <el-form-item label="专科院校ID" prop="vocationalCollegeId">
          <el-input v-model="form.vocationalCollegeId" placeholder="请输入专科院校ID" />
        </el-form-item>
        <el-form-item label="专科专业ID" prop="vocationalMajorId">
          <el-input v-model="form.vocationalMajorId" placeholder="请输入专科专业ID" />
        </el-form-item>
        <el-form-item label="考生类型ID" prop="candidateTypeId">
          <el-input v-model="form.candidateTypeId" placeholder="请输入考生类型ID" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="通知书邮寄地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入通知书邮寄地址" />
        </el-form-item>
        <el-form-item label="通知书收件人" prop="recipientName">
          <el-input v-model="form.recipientName" placeholder="请输入通知书收件人" />
        </el-form-item>
        <el-form-item label="邮编" prop="postalCode">
          <el-input v-model="form.postalCode" placeholder="请输入邮编" />
        </el-form-item>
        <el-form-item label="考点盟市ID" prop="examCenterCityId">
          <el-input v-model="form.examCenterCityId" placeholder="请输入考点盟市ID" />
        </el-form-item>
        <el-form-item label="报考专业ID" prop="examMajorId">
          <el-input v-model="form.examMajorId" placeholder="请输入报考专业ID" />
        </el-form-item>
        <el-form-item label="志愿院校ID" prop="volunteerCollegeId">
          <el-input v-model="form.volunteerCollegeId" placeholder="请输入志愿院校ID" />
        </el-form-item>
        <el-form-item label="志愿专业ID" prop="volunteerMajorId">
          <el-input v-model="form.volunteerMajorId" placeholder="请输入志愿专业ID" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Candidate">
import { listCandidate, getCandidate, delCandidate, addCandidate, updateCandidate } from "@/api/baoming/candidate";

const { proxy } = getCurrentInstance();

const candidateList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    idNumber: null,
    name: null,
    gender: null,
    birthDate: null,
    ethnicityId: null,
    politicalStatusId: null,
    foreignLanguageId: null,
    vocationalCollegeId: null,
    vocationalMajorId: null,
    candidateTypeId: null,
    phone: null,
    address: null,
    isCertificateIssued: null,
    recipientName: null,
    postalCode: null,
    examCenterCityId: null,
    examMajorId: null,
    volunteerCollegeId: null,
    volunteerMajorId: null
  },
  rules: {
    idNumber: [
      { required: true, message: "身份证号不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "姓名不能为空", trigger: "blur" }
    ],
    gender: [
      { required: true, message: "性别不能为空", trigger: "blur" }
    ],
    birthDate: [
      { required: true, message: "出生日期不能为空", trigger: "blur" }
    ],
    ethnicityId: [
      { required: true, message: "民族ID不能为空", trigger: "blur" }
    ],
    politicalStatusId: [
      { required: true, message: "政治面貌ID不能为空", trigger: "blur" }
    ],
    foreignLanguageId: [
      { required: true, message: "外语语言ID不能为空", trigger: "blur" }
    ],
    vocationalCollegeId: [
      { required: true, message: "专科院校ID不能为空", trigger: "blur" }
    ],
    vocationalMajorId: [
      { required: true, message: "专科专业ID不能为空", trigger: "blur" }
    ],
    candidateTypeId: [
      { required: true, message: "考生类型ID不能为空", trigger: "blur" }
    ],
    phone: [
      { required: true, message: "联系电话不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询考生信息列表 */
function getList() {
  loading.value = true;
  listCandidate(queryParams.value).then(response => {
    candidateList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    idNumber: null,
    name: null,
    gender: null,
    birthDate: null,
    ethnicityId: null,
    politicalStatusId: null,
    foreignLanguageId: null,
    vocationalCollegeId: null,
    vocationalMajorId: null,
    candidateTypeId: null,
    phone: null,
    address: null,
    isCertificateIssued: null,
    recipientName: null,
    postalCode: null,
    examCenterCityId: null,
    examMajorId: null,
    volunteerCollegeId: null,
    volunteerMajorId: null
  };
  proxy.resetForm("candidateRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加考生信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCandidate(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改考生信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["candidateRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCandidate(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCandidate(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除考生信息编号为"' + _ids + '"的数据项？').then(function() {
    return delCandidate(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('baoming/candidate/export', {
    ...queryParams.value
  }, `candidate_${new Date().getTime()}.xlsx`)
}

getList();
</script>
